/*
 * Copyright (C) 2004, Velocityme Partnership
 * FileAttachmentSessionBean.java
 *
 * Created on 11 December 2004, 08:53
 */

package com.velocityme.session;

import com.velocityme.interfaces.FileAttachmentLocal;
import com.velocityme.interfaces.FileAttachmentUtil;
import com.velocityme.utility.ServerConfiguration;
import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.ejb.*;
import javax.naming.NamingException;

/**
 *
 * @author  Robert
 * @ejb.bean
 *           type="Stateless"
 *           cmp-version="2.x"
 *           name="FileAttachmentSession"
 *           jndi-name="ejb/FileAttachmentSession"
 *           view-type="local"
 *           transaction-type="Container"
 * @ejb.transaction type="Required"
 *
 * @ejb.util generate="physical"
 */
public class FileAttachmentSessionBean implements SessionBean {
    
    private SessionContext m_context;
    
    /**
     * Helper method which creates a file attachment.
     *
     * @param p_key User's security key
     * @param p_parentNode The node which will contain the new Action
     * @param p_actionValue The value details for the Action part of the action
     *
     * @ejb.interface-method view-type="local"
     **/
    public com.velocityme.interfaces.FileAttachmentLocal createFileAttachment(
                                    com.velocityme.interfaces.ChangeDeltaLocal p_changeDeltaLocal,
                                    java.io.File p_file,
                                    java.lang.String p_contentType,
                                    java.io.InputStream p_stream) {
        try {
            if (p_file != null && !p_file.getName().equals("")) {
                FileAttachmentLocal fileAttachmentLocal = FileAttachmentUtil.getLocalHome().create(p_file.getName(), p_contentType, p_changeDeltaLocal);
                File file = fileAttachmentLocal.getFile();
		if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdir();
                }
                //File file2 = new File(file.getPath() + ".gz");
                //write the file to the file specified
                try {
                    OutputStream bos = new FileOutputStream(file);
                    //GZIPOutputStream gos = new GZIPOutputStream(new FileOutputStream(file2));
                    int bytesRead = 0;
                    byte[] buffer = new byte[8192];
                    while ((bytesRead = p_stream.read(buffer, 0, 8192)) != -1) {
                        bos.write(buffer, 0, bytesRead);
                        //gos.write(buffer, 0, bytesRead);
                    }
                    p_stream.close();
                    bos.close();
                    //gos.finish();
                    //gos.close();
                    fileAttachmentLocal.setLength(new Long(file.length()));
                    // now check the compressed file to see if the space saving is significant
                    /*if (file.length() * 0.8 > file2.length()) {
                        // keep only the compressed file and mark that it is compressed
                        file.delete();
                        file2.renameTo(file);
                        fileAttachmentLocal.setIsCompressed(Boolean.TRUE);
                    }
                    else {
                        // don't bother to keep the compressed file
                        file2.delete();
                    }*/
                }
                catch(FileNotFoundException e) {
                    return null;
                }
                catch(IOException e) {
                    return null;
                }
                return fileAttachmentLocal;
            }
            else {
                return null;
            }
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * Get the length of the attachment to write.
     *
     * @param p_fileAttachmentLocal The file attachment
     *
     * @ejb.interface-method view-type="local"
     **/
    public long fileAttachmentLength(com.velocityme.interfaces.FileAttachmentLocal p_fileAttachmentLocal) throws FileNotFoundException, IOException {
        if (p_fileAttachmentLocal.getLength().longValue() != -1)
            return p_fileAttachmentLocal.getLength().longValue();
        // this is provided for backward compatability!
        File file = p_fileAttachmentLocal.getFile();
        return file.length();
    }
    
    
    /**
     * Get the attachment to write as a buffered input stream.
     *
     * @param p_fileAttachmentLocal The file attachment to be written
     *
     * @ejb.interface-method view-type="local"
     **/
    public java.io.BufferedInputStream fileAttachmentInputStream(com.velocityme.interfaces.FileAttachmentLocal p_fileAttachmentLocal) throws FileNotFoundException, IOException {
        InputStream fileInputStream = new FileInputStream(p_fileAttachmentLocal.getFile());
        if (p_fileAttachmentLocal.getIsCompressed().booleanValue() == true)
            fileInputStream = new GZIPInputStream(fileInputStream);
        return new BufferedInputStream(fileInputStream);
    }
    
    /**
     * Create the Session Bean.
     * @throws CreateException 
     */
    public void ejbCreate() throws CreateException {
    }

    public void ejbActivate() throws java.rmi.RemoteException {
    }
    
    public void ejbPassivate() throws java.rmi.RemoteException {
    }
    
    public void ejbRemove() throws java.rmi.RemoteException {
    }
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) throws java.rmi.RemoteException {
        m_context = sessionContext;
    }
    
}
